
function InitPkg() {
    cargo install protobuf
}
export PATH=$PATH:/home/debi/.cargo/bin

base_dir=$(
  cd "$(dirname "$0")"
  pwd
)
function logger() {
      front=$1
      back=$2
      content=$3
      echo -e "\e["$back";"$front"m"$content"\e[0m"
}
cd $base_dir
logger 33 40 "Current $base_dir"

function ParseTogether() {
    protoPath=$1
    extraDirPrefix=$2
    extraDirMap=$(declare -p "$3")
    declare -A targetMap=${extraDirMap#*=}
    logger 34 40 "your extraDir: targetMap"
    for absPath in $base_dir/$protoPath/* ; do
        if [[ $absPath == *".proto" ]]; then
            fileName=$(basename $absPath)
            fileNameNoExt=$(echo $fileName | cut -d . -f1)
            fileDir=$(dirname $absPath )
            goOutDir=$base_dir/$extraDirPrefix/$fileNameNoExt # dir/*.pd.go
            mkdir -p $goOutDir
             for key in ${!targetMap[*]};do
                    value=${targetMap[$key]}
                    goOutExtraDir=$base_dir/"$extraDirPrefix"/"$value"
                    mkdir -p $goOutExtraDir
                    if [ ! -f "$goOutExtraDir"/$fileNameNoExt."$key".go ]; then
                              echo "package "$value"" > $goOutExtraDir/"$fileNameNoExt"."$key".go
                              logger 36 40 "$goOutExtraDir/"$fileNameNoExt"."$key".go create!"
                    fi
             done
              logger 36 40 $absPath
                protoc -I ../.  -I=.  -I=./..  \
                             --proto_path=$base_dir/$protoPath  \
                              --rust_out $goOutDir --rust_opt paths=source_relative \
                               --go-grpc_out $goOutDir --go-grpc_opt paths=source_relative \
                               --swagger_out logtostderr=true:$goOutDir \
                                  $fileName
      fi
    done
}

#   protoPath=$1
#    extraDirPrefix=$2
#proto相对路径 输出相对路径 要创建的文件夹
declare -A def=( )
declare -A mediaMap=( ["srv"]="serverSrv"  ["router"]="serverRouter"  )
ParseTogether "proto" "proto/entity" def


